package org.yang.faq.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yang.faq.model.Comment;
import org.yang.faq.model.Praise;
import org.yang.faq.service.PraiseService;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api
@Controller
@RequestMapping("/PraiseController")
public class PraiseController {
	@Autowired
	private PraiseService praiseService;

	@ApiOperation(value = "评论查询", notes = "根据点赞数返回统计结果")
	@RequestMapping(value = "/Praise", method = RequestMethod.GET)
	@ResponseBody
	public Page<Praise> findAll(@RequestParam(value = "pId", required = false) String pId) {
		Page<Praise> page = new Page<Praise>(1, 100);
		EntityWrapper wrapper = new EntityWrapper<Comment>();
		if (pId == null) {
			wrapper.orderBy("create_time");
		} else {
			wrapper.eq("p_id", pId).orderBy("create_time");
		}

		page = praiseService.selectPage(page, wrapper);

		return page;
	}

	@ApiOperation(value = "点赞", notes = "一个节目一个微信号只能一次赞")
	@RequestMapping(value = "/Praise", method = RequestMethod.POST)
	@ResponseBody
	public String doPraise(@RequestParam(value = "id", required = false) String id,
			@RequestParam(value = "pId", required = false) String pId,
			@RequestParam(value = "wxId", required = false) String wxId) {
		Praise praise = new Praise();
		praise.setpId(pId);
		praise.setWxId(wxId);
		praise.setCreateTime(new Date());
		
		return praiseService.doPraise(praise);
	}
}
